package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.SensitiveManagementMapper;
import com.heima.wemedia.service.SensitiveManagementService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SensitiveManagementServiceImpl extends ServiceImpl<SensitiveManagementMapper, WmSensitive> implements SensitiveManagementService {

    private final SensitiveManagementMapper sensitiveManagementMapper;

    /**
     * 查询列表
     *
     * @param sensitiveDto
     * @return
     */
    @Override
    public PageResponseResult select(WmSensitiveDto sensitiveDto) {
        IPage page = new Page<>(sensitiveDto.getPage(), sensitiveDto.getSize());
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(WmSensitive::getSensitives, sensitiveDto.getName())
                .orderByDesc(WmSensitive::getCreatedTime);

        sensitiveManagementMapper.selectPage(page, queryWrapper);

        PageResponseResult result = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 保存
     *
     * @param wmSensitive
     */
    @Override
    public void sensitiveWord(WmSensitive wmSensitive) {
        wmSensitive.setCreatedTime(new Date());
        sensitiveManagementMapper.insert(wmSensitive);
    }

    /**
     * 修改
     *
     * @param wmSensitive
     */
    @Override
    public void updateSensitive(WmSensitive wmSensitive) {
        sensitiveManagementMapper.updateById(wmSensitive);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        sensitiveManagementMapper.deleteById(id);
    }
}
